<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>My Project: RateLimiter.h 源文件</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">My Project
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- 制作者 Doxygen 1.9.8 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
  initMenu('',true,false,'search.php','搜索');
  $(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function() { init_codefold(0); });
/* @license-end */
</script>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">载入中...</div>
<div class="SRStatus" id="Searching">搜索中...</div>
<div class="SRStatus" id="NoMatches">未找到</div>
</div>
</div>
</div>
</div>

<div class="header">
  <div class="headertitle"><div class="title">RateLimiter.h</div></div>
</div><!--header-->
<div class="contents">
<a href="RateLimiter_8h.html">浏览该文件的文档.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno">    1</span><span class="preprocessor">#pragma once</span></div>
<div class="line"><a id="l00002" name="l00002"></a><span class="lineno">    2</span><span class="preprocessor">#include &lt;<a class="code" href="exports_8h.html">drogon/exports.h</a>&gt;</span></div>
<div class="line"><a id="l00003" name="l00003"></a><span class="lineno">    3</span><span class="preprocessor">#include &lt;memory&gt;</span></div>
<div class="line"><a id="l00004" name="l00004"></a><span class="lineno">    4</span><span class="preprocessor">#include &lt;chrono&gt;</span></div>
<div class="line"><a id="l00005" name="l00005"></a><span class="lineno">    5</span><span class="preprocessor">#include &lt;mutex&gt;</span></div>
<div class="line"><a id="l00006" name="l00006"></a><span class="lineno">    6</span><span class="preprocessor">#include &lt;string&gt;</span></div>
<div class="line"><a id="l00007" name="l00007"></a><span class="lineno">    7</span> </div>
<div class="line"><a id="l00008" name="l00008"></a><span class="lineno">    8</span><span class="keyword">namespace </span><a class="code hl_namespace" href="namespacedrogon.html">drogon</a></div>
<div class="line"><a id="l00009" name="l00009"></a><span class="lineno">    9</span>{</div>
<div class="line"><a id="l00010" name="l00010"></a><span class="lineno">   10</span><span class="keyword">enum class</span> <a class="code hl_define" href="exports_8h.html#a00d0a001c2dd03906272e00b0598182e">DROGON_EXPORT</a> RateLimiterType</div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno">   11</span>{</div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno"><a class="line" href="RateLimiter_8h.html#aefce0658eedfcdb98296279c6ca48ca9">   12</a></span>    <a class="code hl_variable" href="RateLimiter_8h.html#aefce0658eedfcdb98296279c6ca48ca9">kFixedWindow</a>,</div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno"><a class="line" href="RateLimiter_8h.html#addb8dcc95bc78e95f6a55762e2a23e62">   13</a></span>    <a class="code hl_variable" href="RateLimiter_8h.html#addb8dcc95bc78e95f6a55762e2a23e62">kSlidingWindow</a>,</div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno">   14</span>    kTokenBucket</div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno">   15</span>};</div>
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno">   16</span> </div>
<div class="foldopen" id="foldopen00017" data-start="{" data-end="}">
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno"><a class="line" href="namespacedrogon.html#a5f74a252dbdc0b56180672dae17f1f52">   17</a></span><span class="keyword">inline</span> RateLimiterType <a class="code hl_function" href="namespacedrogon.html#a5f74a252dbdc0b56180672dae17f1f52">stringToRateLimiterType</a>(<span class="keyword">const</span> std::string &amp;type)</div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno">   18</span>{</div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno">   19</span>    <span class="keywordflow">if</span> (type == <span class="stringliteral">&quot;fixedWindow&quot;</span> || type == <span class="stringliteral">&quot;fixed_window&quot;</span>)</div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno">   20</span>        <span class="keywordflow">return</span> RateLimiterType::kFixedWindow;</div>
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno">   21</span>    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (type == <span class="stringliteral">&quot;slidingWindow&quot;</span> || type == <span class="stringliteral">&quot;sliding_window&quot;</span>)</div>
<div class="line"><a id="l00022" name="l00022"></a><span class="lineno">   22</span>        <span class="keywordflow">return</span> RateLimiterType::kSlidingWindow;</div>
<div class="line"><a id="l00023" name="l00023"></a><span class="lineno">   23</span>    <span class="keywordflow">return</span> RateLimiterType::kTokenBucket;</div>
<div class="line"><a id="l00024" name="l00024"></a><span class="lineno">   24</span>}</div>
</div>
<div class="line"><a id="l00025" name="l00025"></a><span class="lineno"><a class="line" href="namespacedrogon.html#aebc80085f1f0fbd9d33321dbaa169c28">   25</a></span><span class="keyword">class </span><a class="code hl_define" href="exports_8h.html#a00d0a001c2dd03906272e00b0598182e">DROGON_EXPORT</a> <a class="code hl_class" href="classdrogon_1_1RateLimiter.html">RateLimiter</a>;</div>
<div class="line"><a id="l00026" name="l00026"></a><span class="lineno"><a class="line" href="namespacedrogon.html#a4c0f7a484aae57628970bf55f539ab5d">   26</a></span><span class="keyword">using </span><a class="code hl_typedef" href="namespacedrogon.html#a4c0f7a484aae57628970bf55f539ab5d">RateLimiterPtr</a> = std::shared_ptr&lt;RateLimiter&gt;;</div>
<div class="line"><a id="l00027" name="l00027"></a><span class="lineno">   27</span> </div>
<div class="foldopen" id="foldopen00032" data-start="{" data-end="};">
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno"><a class="line" href="classdrogon_1_1RateLimiter.html">   32</a></span><span class="keyword">class </span><a class="code hl_define" href="exports_8h.html#a00d0a001c2dd03906272e00b0598182e">DROGON_EXPORT</a> <a class="code hl_class" href="classdrogon_1_1RateLimiter.html">RateLimiter</a></div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno">   33</span>{</div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno">   34</span>  <span class="keyword">public</span>:</div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno"><a class="line" href="classdrogon_1_1RateLimiter.html#a82c37ef918c9116b29b843a4ee26a84c">   42</a></span>    <span class="keyword">static</span> <a class="code hl_typedef" href="namespacedrogon.html#a4c0f7a484aae57628970bf55f539ab5d">RateLimiterPtr</a> <a class="code hl_function" href="classdrogon_1_1RateLimiter.html#a82c37ef918c9116b29b843a4ee26a84c">newRateLimiter</a>(</div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno">   43</span>        RateLimiterType type,</div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno">   44</span>        <span class="keywordtype">size_t</span> capacity,</div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno">   45</span>        std::chrono::duration&lt;double&gt; timeUnit = std::chrono::seconds(60));</div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno"><a class="line" href="classdrogon_1_1RateLimiter.html#ade4b358a0031fe9fc38c96520683e7a8">   52</a></span>    <span class="keyword">virtual</span> <span class="keywordtype">bool</span> <a class="code hl_function" href="classdrogon_1_1RateLimiter.html#ade4b358a0031fe9fc38c96520683e7a8">isAllowed</a>() = 0;</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno"><a class="line" href="classdrogon_1_1RateLimiter.html#a0b215093286124679cf442eeae189682">   53</a></span>    <span class="keyword">virtual</span> <a class="code hl_function" href="classdrogon_1_1RateLimiter.html#a0b215093286124679cf442eeae189682">~RateLimiter</a>() noexcept = default;</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno">   54</span>};</div>
</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno">   55</span> </div>
<div class="foldopen" id="foldopen00056" data-start="{" data-end="};">
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"><a class="line" href="classdrogon_1_1SafeRateLimiter.html">   56</a></span>class <a class="code hl_define" href="exports_8h.html#a00d0a001c2dd03906272e00b0598182e">DROGON_EXPORT</a> <a class="code hl_class" href="classdrogon_1_1SafeRateLimiter.html">SafeRateLimiter</a> : public <a class="code hl_class" href="classdrogon_1_1RateLimiter.html">RateLimiter</a></div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno">   57</span>{</div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno">   58</span>  <span class="keyword">public</span>:</div>
<div class="foldopen" id="foldopen00059" data-start="{" data-end="}">
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno"><a class="line" href="classdrogon_1_1SafeRateLimiter.html#a483f45aae1863551319493e08911815d">   59</a></span>    <a class="code hl_function" href="classdrogon_1_1SafeRateLimiter.html#a483f45aae1863551319493e08911815d">SafeRateLimiter</a>(<a class="code hl_typedef" href="namespacedrogon.html#a4c0f7a484aae57628970bf55f539ab5d">RateLimiterPtr</a> limiter) : limiter_(limiter)</div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno">   60</span>    {</div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno">   61</span>    }</div>
</div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno">   62</span> </div>
<div class="foldopen" id="foldopen00063" data-start="{" data-end="}">
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"><a class="line" href="classdrogon_1_1SafeRateLimiter.html#a7418bc585c3c049285fb5b830b9a791c">   63</a></span>    <span class="keywordtype">bool</span> <a class="code hl_function" href="classdrogon_1_1SafeRateLimiter.html#a7418bc585c3c049285fb5b830b9a791c">isAllowed</a>()<span class="keyword"> override</span></div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno">   64</span><span class="keyword">    </span>{</div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno">   65</span>        std::lock_guard&lt;std::mutex&gt; lock(mutex_);</div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno">   66</span>        <span class="keywordflow">return</span> limiter_-&gt;isAllowed();</div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno">   67</span>    }</div>
</div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno">   68</span> </div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"><a class="line" href="classdrogon_1_1SafeRateLimiter.html#af77d27254ed84509667c1c570bbf6414">   69</a></span>    <a class="code hl_function" href="classdrogon_1_1SafeRateLimiter.html#af77d27254ed84509667c1c570bbf6414">~SafeRateLimiter</a>() noexcept override = default;</div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno">   70</span> </div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno">   71</span>  private:</div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno">   72</span>    <a class="code hl_typedef" href="namespacedrogon.html#a4c0f7a484aae57628970bf55f539ab5d">RateLimiterPtr</a> limiter_;</div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno">   73</span>    std::mutex mutex_;</div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno">   74</span>};</div>
</div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno">   75</span>}  <span class="comment">// namespace drogon</span></div>
<div class="ttc" id="aRateLimiter_8h_html_addb8dcc95bc78e95f6a55762e2a23e62"><div class="ttname"><a href="RateLimiter_8h.html#addb8dcc95bc78e95f6a55762e2a23e62">kSlidingWindow</a></div><div class="ttdeci">kSlidingWindow</div><div class="ttdef"><b>Definition</b> RateLimiter.h:13</div></div>
<div class="ttc" id="aRateLimiter_8h_html_aefce0658eedfcdb98296279c6ca48ca9"><div class="ttname"><a href="RateLimiter_8h.html#aefce0658eedfcdb98296279c6ca48ca9">kFixedWindow</a></div><div class="ttdeci">kFixedWindow</div><div class="ttdef"><b>Definition</b> RateLimiter.h:12</div></div>
<div class="ttc" id="aclassdrogon_1_1RateLimiter_html"><div class="ttname"><a href="classdrogon_1_1RateLimiter.html">drogon::RateLimiter</a></div><div class="ttdoc">This class is used to limit the number of requests per second</div><div class="ttdef"><b>Definition</b> RateLimiter.h:33</div></div>
<div class="ttc" id="aclassdrogon_1_1RateLimiter_html_a0b215093286124679cf442eeae189682"><div class="ttname"><a href="classdrogon_1_1RateLimiter.html#a0b215093286124679cf442eeae189682">drogon::RateLimiter::~RateLimiter</a></div><div class="ttdeci">virtual ~RateLimiter() noexcept=default</div></div>
<div class="ttc" id="aclassdrogon_1_1RateLimiter_html_a82c37ef918c9116b29b843a4ee26a84c"><div class="ttname"><a href="classdrogon_1_1RateLimiter.html#a82c37ef918c9116b29b843a4ee26a84c">drogon::RateLimiter::newRateLimiter</a></div><div class="ttdeci">static RateLimiterPtr newRateLimiter(RateLimiterType type, size_t capacity, std::chrono::duration&lt; double &gt; timeUnit=std::chrono::seconds(60))</div><div class="ttdoc">Create a rate limiter</div></div>
<div class="ttc" id="aclassdrogon_1_1RateLimiter_html_ade4b358a0031fe9fc38c96520683e7a8"><div class="ttname"><a href="classdrogon_1_1RateLimiter.html#ade4b358a0031fe9fc38c96520683e7a8">drogon::RateLimiter::isAllowed</a></div><div class="ttdeci">virtual bool isAllowed()=0</div><div class="ttdoc">Check if a request is allowed</div></div>
<div class="ttc" id="aclassdrogon_1_1SafeRateLimiter_html"><div class="ttname"><a href="classdrogon_1_1SafeRateLimiter.html">drogon::SafeRateLimiter</a></div><div class="ttdef"><b>Definition</b> RateLimiter.h:57</div></div>
<div class="ttc" id="aclassdrogon_1_1SafeRateLimiter_html_a483f45aae1863551319493e08911815d"><div class="ttname"><a href="classdrogon_1_1SafeRateLimiter.html#a483f45aae1863551319493e08911815d">drogon::SafeRateLimiter::SafeRateLimiter</a></div><div class="ttdeci">SafeRateLimiter(RateLimiterPtr limiter)</div><div class="ttdef"><b>Definition</b> RateLimiter.h:59</div></div>
<div class="ttc" id="aclassdrogon_1_1SafeRateLimiter_html_a7418bc585c3c049285fb5b830b9a791c"><div class="ttname"><a href="classdrogon_1_1SafeRateLimiter.html#a7418bc585c3c049285fb5b830b9a791c">drogon::SafeRateLimiter::isAllowed</a></div><div class="ttdeci">bool isAllowed() override</div><div class="ttdoc">Check if a request is allowed</div><div class="ttdef"><b>Definition</b> RateLimiter.h:63</div></div>
<div class="ttc" id="aclassdrogon_1_1SafeRateLimiter_html_af77d27254ed84509667c1c570bbf6414"><div class="ttname"><a href="classdrogon_1_1SafeRateLimiter.html#af77d27254ed84509667c1c570bbf6414">drogon::SafeRateLimiter::~SafeRateLimiter</a></div><div class="ttdeci">~SafeRateLimiter() noexcept override=default</div></div>
<div class="ttc" id="aexports_8h_html"><div class="ttname"><a href="exports_8h.html">exports.h</a></div></div>
<div class="ttc" id="aexports_8h_html_a00d0a001c2dd03906272e00b0598182e"><div class="ttname"><a href="exports_8h.html#a00d0a001c2dd03906272e00b0598182e">DROGON_EXPORT</a></div><div class="ttdeci">#define DROGON_EXPORT</div><div class="ttdef"><b>Definition</b> exports.h:15</div></div>
<div class="ttc" id="anamespacedrogon_html"><div class="ttname"><a href="namespacedrogon.html">drogon</a></div><div class="ttdoc">Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...</div><div class="ttdef"><b>Definition</b> Attribute.h:23</div></div>
<div class="ttc" id="anamespacedrogon_html_a4c0f7a484aae57628970bf55f539ab5d"><div class="ttname"><a href="namespacedrogon.html#a4c0f7a484aae57628970bf55f539ab5d">drogon::RateLimiterPtr</a></div><div class="ttdeci">std::shared_ptr&lt; RateLimiter &gt; RateLimiterPtr</div><div class="ttdef"><b>Definition</b> RateLimiter.h:26</div></div>
<div class="ttc" id="anamespacedrogon_html_a5f74a252dbdc0b56180672dae17f1f52"><div class="ttname"><a href="namespacedrogon.html#a5f74a252dbdc0b56180672dae17f1f52">drogon::stringToRateLimiterType</a></div><div class="ttdeci">RateLimiterType stringToRateLimiterType(const std::string &amp;type)</div><div class="ttdef"><b>Definition</b> RateLimiter.h:17</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
制作者&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.8
</small></address>
</body>
</html>
